<template>
  <div class="box">
    <h2>
      商品分类页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增类别</el-button
        ></small
      >
    </h2>
    <!-- default-expand-all 默认展开全部 子菜单 -->
    <el-table
      :data="cates.cateList"
      row-key="id"
      width="100%"
      height="600"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column prop="pid" label="上级编号" width="80">
      </el-table-column>
      <el-table-column prop="catename" label="分类名称" width="200">
      </el-table-column>
      <el-table-column prop="img" label="封面图片">
        <template v-slot="props">
          <el-avatar
            v-if="props.row.pid !== 0"
            shape="square"
            :size="100"
            fit="cover"
            :src="'http://localhost:3000' + props.row.img"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="props">
          <el-tag size="small" v-if="props.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="small"
            round
            @click="editCate(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="delCate(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
// 导入store
import useCateStore from "../../stores/cate.js";
// 导入弹窗组件
import Dialog from "./dialog.vue";

const cates = useCateStore();

const $router = useRouter();

// 获取数据
onMounted(() => {
  // 调用函数,获取分类数据
  cates.getCateListActions();
});

// 弹窗变量
const VDialog = ref(null);

// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};

//删除类别
function delCate(Cates) {
  console.log("Cates", Cates);
  if (Cates.children) {
    ElMessage({
      type: "warning",
      message: "你不能删除带有子类的分类",
    });
    return;
  }
  ElMessageBox.confirm("确定删除此类别吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      cates.delCateActions(Cates.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此类别",
      });
    });
}
// 编辑类别
function editCate(Cates) {
  // console.log("11111111111111", VDialog.value, Menus.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Cates);
  showDialog();
}
</script>

<style lang="css" scoped></style>
